package Solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-20
 * Time: 15:23
 */

//https://www.nowcoder.com/practice/09fbfb16140b40499951f55113f2166c?tpId=188&&tqId=38559&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking

public class Solution1 {
    /**
     * @param x int整型
     * @return int整型
     */
    public int sqrt(int x) {
        // write code here
        return (int) sqrt((double) x, 0.1);
    }

    //百度百科：《牛顿迭代法》

    /**
     * a 为开平方的值
     * b 为精度
     */
    private double sqrt(double a, double b) {
        double x = 1.0;
        double check;
        do {
            x = (x + a / x) * 0.5;
            check = x * x - a;//判断是精度是否符合要求
        } while ((check > 0 ? check : -check) > b);
        return x;
    }
}
